package com.example.textgradient;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class ColorTrackTextView extends AppCompatTextView {

    private Paint changePaint;
    private Paint originPaint;
    private float mCurrentProgress = 0.0f;

    private Direction mDirection = Direction.LEFT_TO_RIGHT;

    public enum Direction {
        LEFT_TO_RIGHT, RIGHT_TO_LEFT
    }

    public ColorTrackTextView(@NonNull Context context) {
        this(context, null);
    }

    public ColorTrackTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorTrackTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        int middle = (int) (mCurrentProgress * getWidth());
        if (mDirection == Direction.LEFT_TO_RIGHT) {
            drawText(canvas, originPaint, middle, getWidth());
            drawText(canvas, changePaint, 0, middle);
        } else {
            drawText(canvas, originPaint, 0, getWidth() - middle);
            drawText(canvas, changePaint, getWidth() - middle, getWidth());
        }


    }

    public void setCurrentProgress(float CurrentProgress) {
        this.mCurrentProgress = CurrentProgress;
        invalidate();
    }

    public void setDirection(Direction direction) {
        this.mDirection = direction;
    }

    public void setChangeColor(int changeColor){
        this.changePaint.setColor(changeColor);
    }
    private void drawText(Canvas canvas, Paint paint, int start, int end) {

        canvas.save();
        Rect rect = new Rect(start, 0, end, getHeight());
        canvas.clipRect(rect);

        String text = getText().toString();
        if (TextUtils.isEmpty(text)) return;

        Rect bounds = new Rect();

        paint.getTextBounds(text, 0, text.length(), bounds);
        int x = getWidth() / 2 - bounds.width() / 2;

        Paint.FontMetricsInt fontMetricsInt = changePaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        int baseLine = getHeight() / 2 + dy;

        canvas.drawText(text, x, baseLine, paint);
        canvas.restore();
    }

    private void initPaint(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView);
        int changeColor = typedArray.getColor(R.styleable.ColorTrackTextView_changeColor, Color.BLUE);
        int originColor = typedArray.getColor(R.styleable.ColorTrackTextView_originColor, getTextColors().getDefaultColor());
        changePaint = getPaintByColor(changeColor);
        originPaint = getPaintByColor(originColor);
        typedArray.recycle();
    }

    private Paint getPaintByColor(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setTextSize(getTextSize());
        return paint;
    }


}
